package com.example.heipictureserver.mangger.websocket.disruptor;

import cn.hutool.json.JSONUtil;
import com.example.heipictureserver.mangger.websocket.PictureEditHandler;
import com.example.heipictureserver.mangger.websocket.modal.PictureEditMessageTypeEnum;
import com.example.heipictureserver.mangger.websocket.modal.PictureEditRequestMessage;
import com.example.heipictureserver.mangger.websocket.modal.PictureEditResponseMessage;
import com.example.heipictureserver.modal.domain.User;
import com.example.heipictureserver.service.UserService;
import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/*
  *@Description 图片编辑事件处理器（消费者）
  *@Author bwg
  *@Date 2025/2/13 21:16
*/
@Component
@Slf4j
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {

 @Resource
 private PictureEditHandler pictureEditHandler;

 @Resource
 private UserService userService;

 @Override
 public void onEvent(PictureEditEvent pictureEditEvent) throws Exception {
  PictureEditRequestMessage pictureEditRequestMessage = pictureEditEvent.getPictureEditRequestMessage();
  WebSocketSession session = pictureEditEvent.getSession();
  User user = pictureEditEvent.getUser();
  String pictureId = pictureEditEvent.getPictureId();
  // 获取到消息类别
  String type = pictureEditRequestMessage.getType();
  PictureEditMessageTypeEnum pictureEditMessageTypeEnum = PictureEditMessageTypeEnum.getEnumByValue(type);
  // 根据消息类型处理消息
  switch (pictureEditMessageTypeEnum) {
   case ENTER_EDIT:
    pictureEditHandler.handleEnterEditMessage(pictureEditRequestMessage, session, user, pictureId);
    break;
   case EXIT_EDIT:
    pictureEditHandler.handleExitEditMessage(pictureEditRequestMessage, session, user, pictureId);
    break;
   case EDIT_ACTION:
    pictureEditHandler.handleEditActionMessage(pictureEditRequestMessage, session, user, pictureId);
    break;
   default:
    // 其他消息类型，返回错误提示
    PictureEditResponseMessage pictureEditResponseMessage = new PictureEditResponseMessage();
    pictureEditResponseMessage.setType(PictureEditMessageTypeEnum.ERROR.getValue());
    pictureEditResponseMessage.setMessage("消息类型错误");
    pictureEditResponseMessage.setUser(userService.getUserVO(user));
    session.sendMessage(new TextMessage(JSONUtil.toJsonStr(pictureEditResponseMessage)));
    break;
  }
 }
}

